package top.lixunda.ecommerce.server.good.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import top.lixunda.common.utils.ObjectValidators;
import top.lixunda.ecommerce.server.business.BaseServiceImpl;
import top.lixunda.ecommerce.server.business.api.entity.cache.CacheUser;
import top.lixunda.ecommerce.server.good.api.entity.db.EnshrineGoodsDO;
import top.lixunda.ecommerce.server.good.api.entity.db.GoodsDO;
import top.lixunda.ecommerce.server.good.api.entity.dto.EnshrineGoodQueryDTO;
import top.lixunda.ecommerce.server.business.api.entity.dto.PageDTO;
import top.lixunda.ecommerce.server.good.mapper.EnshrineGoodsMapper;
import top.lixunda.ecommerce.server.good.service.IEnshrineGoodsService;
import top.lixunda.ecommerce.server.good.service.IGoodsService;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 店铺收藏 服务实现类
 * </p>
 *
 * @author Xumda
 * @since 2020-01-06
 */
@Slf4j
@Service
public class EnshrineGoodsServiceImpl extends BaseServiceImpl<EnshrineGoodsMapper, EnshrineGoodsDO> implements IEnshrineGoodsService {

    private final IGoodsService goodsService;

    @Autowired
    public EnshrineGoodsServiceImpl(IGoodsService goodsService) {
        this.goodsService = goodsService;
    }

    @Override
    public Integer addEnshrineGoods(List<Integer> ids) {
        if (ObjectValidators.isEmpty(ids)) {
            throw getAppException("参数不能为空");
        }

        List<EnshrineGoodsDO> list = new ArrayList<>();
        EnshrineGoodsDO goodsDO;
        CacheUser userInfoDO = currentUserCacheNotNull();

        // 根据id查询商品名称
        List<GoodsDO> goodsList = goodsService.list(
                new LambdaQueryWrapper<GoodsDO>()
                        .in(GoodsDO::getId, ids));

        Map<Integer, String> nameMap = new HashMap<>(goodsList.size());
        for (GoodsDO g : goodsList) {
            nameMap.put(g.getId(), g.getName());
        }

        for (Integer id : ids) {
            goodsDO = new EnshrineGoodsDO();
            goodsDO.setUserId(userInfoDO.getAccount());
            goodsDO.setUserName(userInfoDO.getName());
            goodsDO.setGoodId(id);
            goodsDO.setGoodName(nameMap.get(id));
            goodsDO.setTime(LocalDateTime.now());
            goodsDO.setUseable(1);
            list.add(goodsDO);
        }

        this.saveBatch(list);
        return list.size();
    }

    @Override
    public Integer removeEnshrineGoods(List<Integer> ids) {
        if (ObjectValidators.isEmpty(ids)) {
            return 0;
        }

        Integer userId = currentUserCacheNotNull().getAccount();

        boolean success = this.remove(
                new LambdaUpdateWrapper<EnshrineGoodsDO>()
                        .eq(EnshrineGoodsDO::getUserId, userId)
                        .in(EnshrineGoodsDO::getId, ids));

        if (!success) {
            throw getAppException("取消收藏失败");
        }
        return ids.size();
    }

    @Override
    public List<EnshrineGoodsDO> queryAllEnshrineGoods() {
        Integer userId = currentUserCacheNotNull().getAccount();
        return this.list(
                new LambdaQueryWrapper<EnshrineGoodsDO>()
                        .eq(EnshrineGoodsDO::getUserId, userId));
    }

    @Override
    public List<EnshrineGoodsDO> queryEnshrineGoodsWrapper(EnshrineGoodQueryDTO enshrineGoodQueryDTO) {
        Integer userId = currentUserCacheNotNull().getAccount();
        return this.list(
                enshrineGoodQueryDTO.toWrapper()
                        .eq(EnshrineGoodsDO::getUserId, userId));
    }

    @Override
    public IPage<EnshrineGoodsDO> queryEnshrineGoodsPage(PageDTO<EnshrineGoodsDO, ?> pageDTO) {
        Integer userId = currentUserCacheNotNull().getAccount();
        return this.page(pageDTO.getPage(), pageDTO.getWrapper(
                wrapper -> wrapper.eq(EnshrineGoodsDO::getUserId, userId)
        ));
    }

    @Override
    public IPage<EnshrineGoodsDO> queryEnshrineGoodsPageWrapper(PageDTO<EnshrineGoodsDO, EnshrineGoodQueryDTO> pageDTO) {
        Integer userId = currentUserCacheNotNull().getAccount();
        return this.page(pageDTO.getPage(), pageDTO.getWrapper(
                wrapper -> wrapper.eq(EnshrineGoodsDO::getUserId, userId)));

    }

    @Override
    public EnshrineGoodsDO queryById(Integer id) {
        if (ObjectValidators.isEmpty(id)) {
            throw getAppException("收藏id不能为空");
        }
        Integer userId = currentUserCacheNotNull().getAccount();
        return this.getOne(
                new LambdaQueryWrapper<EnshrineGoodsDO>()
                        .eq(EnshrineGoodsDO::getUserId, userId)
                        .eq(EnshrineGoodsDO::getId, id), false);
    }

    @Override
    public List<EnshrineGoodsDO> queryByIds(List<Integer> ids) {
        if (ObjectValidators.isEmpty(ids)) {
            throw getAppException("收藏id列表不能为空");
        }
        Integer userId = currentUserCacheNotNull().getAccount();
        return this.list(
                new LambdaQueryWrapper<EnshrineGoodsDO>()
                        .eq(EnshrineGoodsDO::getUserId, userId)
                        .in(EnshrineGoodsDO::getId, ids));
    }

}
